<template>
  <div id="show-blog"  v-theme:column="'wide'">
    <h1>博客总览</h1>
    <input type="text" class="form-control" placeholder="搜索" v-model="search">
    <div class="single-blog" v-for="blog in filteredBlogs" :key="blog.id">
      <router-link v-bind:to="'/blog/'+blog.id">
        <h2 v-rainbow>{{blog.title | to-uppercase}}</h2>
      </router-link>
      <article>{{blog.content | snippet}}</article> 
    </div>

  </div>
</template>

<script>
export default {
  name: "show-blog",
  data() {
    return {
      blogs: [],
      search:''
    };
  },
  created() {
    // this.$http.get("https://jsonplaceholder.typicode.com/posts").then(data => {
    //   //  console.log(data)
    //   this.blogs = data.body.slice(0, 10);
    //   console.log(this.blogs);
    // });
    this.$http.get("https://vuedemo-123.firebaseio.com//posts.json").then(data => {
      //  console.log(data.body)
       return data.json()
      // this.blogs = data.body.slice(0, 10);
      // console.log(this.blogs);
    })
    .then(function(data){
      var blogArray=[];
      for(let key in data){
        blogArray.push(data[key])
        data[key].id=key;
        console.log(blogArray)
        this.blogs=blogArray;
      }
    });
  },
  computed:{
    filteredBlogs:function(){
      return this.blogs.filter((blog)=>{
        return blog.title.match(this.search)
      })
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
#show-blog {
  max-width: 800px;
  margin: 0 auto;
}
.single-blog {
  padding: 20px;
  margin: 20px 0;
  box-sizing: border-box;
  background: #eee;
}
#show-blog a{
  text-decoration: none;
}
</style>
